<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class BalanceWater extends \app\BaseModel
{
    protected $name = "massage_service_balance_water";
    protected $append = ["goods_title"];
    public function getGoodsTitleAttr($value, $data)
    {
        if(!empty($data["type"]) && !empty($data["order_id"])) {
            $balance_order_model = new BalanceOrder();
            if(in_array($data["type"], [1, 5])) {
                $title = $balance_order_model->where(["id" => $data["order_id"]])->value("title");
            } else {
                $order_goods_model = $data["type"] == 4 ? new UpOrderList() : new Order();
                if($data["type"] == 6) {
                    $order_goods_model = new UpOrderList();
                }
                $title = $order_goods_model->where(["id" => $data["order_id"]])->value("order_code");
                $title = "订单号:" . $title;
            }
            return $title;
        }
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page = 10)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function updateUserBalance($order, $type, $add = 0)
    {
        $user_model = new User();
        $water_model = new BalanceWater();
        $user = $user_model->dataInfo(["id" => $order["user_id"]]);
        if(in_array($type, [1, 5])) {
            $order["pay_price"] = abs($order["true_price"]);
        }
        if($add == 0) {
            $res = $user_model->dataUpdate(["id" => $order["user_id"], "lock" => $user["lock"]], ["balance" => $user["balance"] - $order["pay_price"], "lock" => $user["lock"] + 1]);
        } else {
            $res = $user_model->dataUpdate(["id" => $order["user_id"], "lock" => $user["lock"]], ["balance" => $user["balance"] + $order["pay_price"], "lock" => $user["lock"] + 1]);
        }
        if($res == 0) {
            return false;
        }
        $insert = ["uniacid" => $order["uniacid"], "user_id" => $order["user_id"], "price" => $order["pay_price"], "order_id" => $order["id"], "add" => $add, "type" => $type, "before_balance" => $user["balance"], "after_balance" => $add == 0 ? $user["balance"] - $order["pay_price"] : $user["balance"] + $order["pay_price"], "text" => $order["text"] ?? ""];
        $res = $water_model->dataAdd($insert);
        return $res;
    }
}

?>